import { defineStore } from 'pinia';
import { ref } from 'vue';

export const useVehicleStore = defineStore('vehicle', () => {
  // 车辆列表
  const vehicleList = ref([]);
  
  // 当前选中的车辆索引
  const currentVehicleIndex = ref(0);
  
  // 设置车辆列表
  const setVehicleList = (list) => {
    vehicleList.value = list;
  };
  
  // 设置当前车辆索引
  const setCurrentVehicleIndex = (index) => {
    currentVehicleIndex.value = index;
  };
  
  // 重置仓库数据（用于退出登录时清理）
  const resetStore = () => {
    vehicleList.value = [];
    currentVehicleIndex.value = 0;
  };
  
  // 获取当前车辆信息
  const getCurrentVehicle = () => {
    if (vehicleList.value.length === 0) {
      return null;
    }
    return vehicleList.value[currentVehicleIndex.value];
  };
  
  return {
    vehicleList,
    currentVehicleIndex,
    setVehicleList,
    setCurrentVehicleIndex,
    resetStore,
    getCurrentVehicle,
  };
}, {
  persist: true, // 数据持久化
});